package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.model.common.admin.dto.AdminDictTypeDto;
import com.zhentao.model.common.admin.pojo.AdminUser;
import com.zhentao.model.common.admin.pojo.DictType;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.DictTypeService;
import com.zhentao.mapper.DictTypeMapper;
import com.zhentao.util.UserThreadLocalutil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author liu
* @description 针对表【dict_type】的数据库操作Service实现
* @createDate 2024-09-27 10:36:10
*/
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType>
    implements DictTypeService{
    @Resource
    private DictTypeMapper dictTypeMapper;

    /**
     * 字典表类型列表
     * @return
     */
    @Override
    public ResponseResult adminDictTypeList(AdminDictTypeDto adminDictTypeDto) {
        PageHelper.startPage(adminDictTypeDto.getPageNum(),adminDictTypeDto.getPageSize());
        List<DictType> dictTypes = dictTypeMapper.selectList(Wrappers.<DictType>lambdaQuery()
                .eq(StringUtils.isNotBlank(adminDictTypeDto.getDictName()), DictType::getDictName, adminDictTypeDto.getDictName())
                .eq(adminDictTypeDto.getStatus() != 0, DictType::getStatus, adminDictTypeDto.getStatus())
        );

        PageInfo<DictType> dictTypePageInfo = new PageInfo<>(dictTypes);

        return ResponseResult.okResult(dictTypePageInfo);
    }

    /**
     * 字典类型---添加-修改-删除
     * @param dictType
     * @return
     */
    @Override
    public ResponseResult adminAddDictType(DictType dictType) {
        //判断是否为空
        if (StringUtils.isNotBlank(dictType.getDictName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"字典名称不能为空");
        }
        if (StringUtils.isNotBlank(dictType.getDictType())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"字典类型不能为空");
        }
        //根据名称以及类型查询
        DictType dictType1 = dictTypeMapper.selectOne(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictName, dictType.getDictName())
        );
        if (dictType1==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"字典名称重复");
        }
        DictType dictType2 = dictTypeMapper.selectOne(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictType, dictType.getDictType())
        );
        if (dictType2==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"字典类型重复");
        }

        AdminUser user = UserThreadLocalutil.getUser();
        String username = user.getUsername();
        if (dictType.getButCode()==1){
            //进行添加
            dictType.setCreateBy(username);
            dictType.setCreateTime(new Date());
            dictTypeMapper.insert(dictType);
            return ResponseResult.okResult("添加成功");
        }else if (dictType.getButCode()==2){
            dictType.setUpdateBy(username);
            dictType.setUpdateTime(new Date());
            dictTypeMapper.updateById(dictType);
            return ResponseResult.okResult("修改成功");
        }else {
            dictTypeMapper.deleteById(dictType.getDictId());
            return ResponseResult.okResult("删除成功");
        }



    }
}




